<?php
declare(strict_types=1);

namespace App\Service;

use App\Helper\Log;
use App\Helper\Util;
use Hyperf\DbConnection\Db;
use App\Helper\Calendar;
use App\Service\TradService;

/**
 * 用户服务
 */
class UserService 
{
    private $user_id = 0;
    /**
     * [__construct description] 构造函数
     */
    public function __construct($user_id=0)
    {
        $this->user_id = $user_id;
    }
    /**
     * 设置全局用户ID
     * [setUserId description]
     * @param [type] $user_id [description]
     */
    public function setUserId($user_id)
    {
        $this->user_id = $user_id;
    }
    /**
     * 获取用户日历
     * [getUserCalendarInfo description]
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function getUserCalendarInfo($params)
    {
        if(empty($params['office_id'])){
            return NULL;
        }
        if(empty($params['begin_date']) || empty($params['end_date'])){
            $cal = Calendar::getWeekJustBeginAndEnd();
            $params['begin_date'] = $cal['first_date'];
            $params['end_date'] = $cal['last_date'];
        }
        $params['user_id'] = $this->user_id;
        $return_data = $this->getOnePersonCal($params);
        return $return_data;
    }
    /**
     * 获取一个人的日历信息
     * [getOnePersonCal description]
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function getOnePersonCal($params)
    {
        $return_data = [
            'total_work_hours' => 0,
            'list' => [],
        ];
        $data = Db::table('office_calendar')
            ->select(['wdate','trad_id'])
            ->where(['is_del'=>0,'status'=>1,'office_id'=>$params['office_id'],'user_id'=>$params['user_id']])
            ->whereBetween('wdate', [$params['begin_date'], $params['end_date']])
            ->orderBy('wdate', 'asc')
            ->get()
            ->toArray();
        $data = array_column($data, NULL, "wdate");
        if(!empty($data)){
            foreach ($data as $c) {
                $rcal = $this->richTradCountWorkTime($c['trad_id']);
                $return_data['list'][] = [
                    'wdate' => $c['wdate'],
                    'list' => $rcal['list']
                ];;
                $return_data['total_work_hours'] += $rcal['total_work_hours'];
            }
        }
        return $return_data;
    }
    /**
     * 检查排班是否已发布
     * [checkPersonCalIsPublic description]
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function checkPersonCalIsPublic($params)
    {
        $data = Db::table('office_calendar')
            ->select(['wdate','trad_id'])
            ->where(['is_del'=>0,'status'=>1,'office_id'=>$params['office_id']])
            ->whereBetween('wdate', [$params['begin_date'], $params['end_date']])
            ->first();
        if(!empty($data)){
            return true;
        }
        return false;
    }
    /**
     * 获取一个人的日历信息-不包含总工时
     * [getOnePersonCalWithoutTotal description]
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function getOnePersonCalWithoutTotal($params)
    {
        $return_data = [];
        $cal = Calendar::getCalByBeginAndEnd($params['begin_date'],$params['end_date']);
        $data = Db::table('office_calendar')
            ->select(['wdate','trad_id'])
            ->where(['is_del'=>0,'status'=>1,'office_id'=>$params['office_id'],'user_id'=>$params['user_id']])
            ->whereBetween('wdate', [$params['begin_date'], $params['end_date']])
            ->orderBy('wdate', 'asc')
            ->get()
            ->toArray();
        $data = array_column($data, NULL, "wdate");
        foreach ($cal as $c) {
            if(!empty($data[$c])){
                $trad_info = $this->richTrad($data[$c]['trad_id']);
                $return_data[] = [
                    'wdate' => $c,
                    'list' => $trad_info
                ];
            }else{
                $return_data[] = [
                    'wdate' => $c,
                    'list' => NULL
                ];
            }
        }
        return $return_data;
    }
    /**
     * 获取一个人的日历信息-不包含总工时-临时表
     * [getOnePersonCalTempWithoutTotal description]
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function getOnePersonCalTempWithoutTotal($params)
    {
        $return_data = [];
        $cal = Calendar::getCalByBeginAndEnd($params['begin_date'],$params['end_date']);
        $data = Db::table('office_calendar_temp')
            ->select(['wdate','trad_id'])
            ->where(['is_del'=>0,'status'=>1,'office_id'=>$params['office_id'],'user_id'=>$params['user_id']])
            ->whereBetween('wdate', [$params['begin_date'], $params['end_date']])
            ->orderBy('wdate', 'asc')
            ->get()
            ->toArray();
        $data = array_column($data, NULL, "wdate");
        foreach ($cal as $c) {
            if(!empty($data[$c])){
                $trad_info = $this->richTrad($data[$c]['trad_id']);
                $return_data[] = [
                    'wdate' => $c,
                    'list' => $trad_info
                ];
            }else{
                $return_data[] = [
                    'wdate' => $c,
                    'list' => NULL
                ];
            }
        }
        return $return_data;
    }
    /**
     * 获取用户某日详情
     * [getUserDayInfo description]
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function getUserDayInfo($params)
    {
        $where = [
            'is_del'=>0,
            'status'=>1,
            'office_id'=>$params['office_id'],
            'user_id'=>$this->user_id,
            'wdate'=>$params['day']
        ];
        $trad_id = Db::table('office_calendar')
            ->where($where)
            ->value("trad_id");
        $data = $this->richTrad($trad_id);
        return $data;
    }
    /**
     * 丰富班次信息-并计算工时
     * [richTrad description]
     * @param  [type] $ids [description]
     * @return [type]      [description]
     */
    public function richTradCountWorkTime($ids)
    {
        $data = self::richTrad($ids);
        $total_work_hours = 0;
        if(!empty($data)){
            foreach ($data as $key => $value) {
                $total_work_hours = bcadd((string)$total_work_hours, (string)$value['work_hours']);
            }
        }
        $return_data = [
            'total_work_hours'=>$total_work_hours,
            'list'=>$data
        ];
        return $return_data;
    }
    /**
     * 丰富班次信息
     * [richTrad description]
     * @param  [type] $ids [description]
     * @return [type]      [description]
     */
    public function richTrad($ids)
    {
        if(empty($ids)){
            return NULL;
        }
        $id_array = explode(",", $ids);
        $id_array = array_filter($id_array);
        if(empty($id_array)){
            return NULL;
        }
        $data = [];
        $tradServ = new TradService();
        foreach ($id_array as $id) {
            if(empty($id)){
                continue;
            }
            $tinfo = $tradServ->getTradInfoMore($id);
            $data[] = [
                'trad_id'=>$id,
                'trad_name'=>$tinfo['name']??"",
                'color_id'=>$tinfo['color']??"",
                'color_code'=>$tinfo['color_code']??"",
                'work_time'=>$tinfo['work_time']??"",
                'work_hours'=>$tinfo['work_hours']??0,
            ];
        }
        return $data;
    }
    /**
     * 获取用户正在使用的科室
     * [getUserInUseOffice description]
     * @return [type] [description]
     */
    public function getUserInUseOffice()
    {
        $data = Db::table('office_member')
            ->select(['office_id','role'])
            ->where(['is_del'=>0,'status'=>1,'in_use'=>1,'user_id'=>$this->user_id])
            ->orderBy('created_at', 'desc')
            ->first();
        return $data;
    }
    /**
     * 检查关注状态
     * [checkSubStatus description]
     * @return [type] [description]
     */
    public function checkSubStatus()
    {
        $return_data = [
            'spb'=>0
        ];
        $user = Db::table('user')
            ->where('id', $this->user_id)
            ->first();
        if(empty($user)){
            return $return_data;
        }
        if(!empty($user['wx_openid'])){
            $user_act = Db::table('wxship_act')
                ->where('openid', $user['wx_openid'])
                ->first();
            if(!empty($user_act['status']) && $user_act['status']==1){
                $return_data['spb'] = 1;
            }
        }elseif(!empty($user['unionid'])){
            $user_act = Db::table('wxship_act')
                ->where('unionid', $user['unionid'])
                ->first();
            if(!empty($user_act['status']) && $user_act['status']==1){
                $return_data['spb'] = 1;
            }
        }
        return $return_data;
    }
}
